        TITL DAI FIRMWARE D8FB-DAD3  V1.1
        ORG   :D8FB
*
*
*
*  ===================
*** INTERRUPT HANDLER ***
*  ===================
*
*
*******************************
* INITIALISE INTERRUPT SYSTEM *
*******************************
*
* Initialises the interrupt system of the machine.
*
* Entry: No conditions.
* Exit:  All registers corrupted.
*
INTINI  LXI   H,TICC8   Address int.mask register
        MVI   A,:04
        MOV   M,A       Only stack interrupt
        STA   CHOPRT    Remember int. mask
        MVI   L,:F4
        MVI   A,:0C
        MOV   M,A       Select ext.int. and INTA
        INR   A
        MOV   M,A       Reset
        MVI   A,:0C
        STA   CURCLK    Init. cursor timer
        MVI   A,:02
        STA   KBXCT     Init. keyboard scan counter
        CALL  SNDIS     Reload sound timer
        CALL  KBIS      Reload keyboard timer
        CALL  VECSU     Set up int. entry points
 REM
* Set-up interrupt vectors (also entry from utility)
 REM
INTSU   LXI   H,CLKINT
        SHLD  I7USA     Clock int. vector (7)
        LXI   H,KBINT
        SHLD  I6USA     Keyboard int. vector (6)
        LXI   H,SCRST
        SHLD  I5USA     Screen restart vector (5)
        LXI   H,MARST
        SHLD  I4USA     Math. restart vector (4)
        LXI   H,SNTMP
        SHLD  I3USA     Sound int. vector (3)
        LXI   H,SPINT
        SHLD  I2USA     Stack int. vector (2)
        LXI   H,UTRST
        SHLD  I1USA     Utility/encode vector (1)
        RET 
*
* SET UP INTERRUPT VECTOR AREA:
*
* Sets up the vector area 0000-003F.
*
* Entry: No conditions.
* Exit:  All registers corrupted.
*
VECSU   LXI   B,:0000   Start at zero
VCS10   LXI   D,ITMPL   Startaddr. int. routine
        LXI   H,KBDI    Endaddress
        CALL  MOVE      Transfer int. routine
        MOV   A,C
        CPI   :40       8 routines done?
        JNZ   VCS10     Next one if not ready
        LXI   H,:003B   Addr. highest int.vector
        LXI   B,TICC8
        MVI   D,:70
VCS20   MOV   M,D       Load vector addr.
        DCR   D
        DCR   D
        DAD   B         Calculate next addr
        JC    VCS20     Next one if not ready
        RET 
*
* INTERRUPT VECTOR ROUTINE:
*
* During initialisation loaded into RAM on the
* Restart routine locations of the CPU.
* The address after LHLD is later changed into
* the appropriate vector address by #D8FB.
*
ITMPL   NOP 
        PUSH  H
        LHLD  I7USA
        PCHL
        NOP 
        NOP 
*
*******************************
* DISABLE KEYBOARD INTERRUPTS *
*******************************
*
* Disables keyboard interrupts only.
*
* Entry can be different. From INTCH common part
* to disable/enable sound, clock and keyboard
* interrupts.
*
* Entry: None.
* Exit:  All registers preserved.
*
KBDI    PUSH  B
        LXI   B,:BF00   Disable keyboard interrupts
*
INTCH   PUSH  PSW
        DI              Disable interrupts
        LDA   CHOPRT    Get current int.mask
        ANA   B         ) Calculate new one
        ORA   C         )
        STA   CHOPRT    Remember new mask
        STA   TICC8     and set mask
        EI              Enable interrupts again
        POP   PSW
        POP   B
        RET 
*
******************************
* ENABLE KEYBOARD INTERRUPTS *
******************************
*
* Enables keyboard interrupts only.
*
KBEI    PUSH  B
        LXI   B,:FF40   Enable keyboard interrupts
        JMP   INTCH     Update int. mask
*
****************************
* DISABLE SOUND INTERRUPTS *
****************************
*
* Disables sound interrupts only.
*
SNDDI   PUSH  B
        LXI   B,:F700   Disable sound interrupts
        JMP   INTCH     Update int. mask
*
***************************
* ENABLE SOUND INTERRUPTS *
***************************
*
* Enables sound interrupts only.
*
SNDEI   PUSH  B
        LXI   B,:FF08   Enable sound interrupts
        JMP   INTCH     Update int. mask
*
***********************
* LOAD KEYBOARD TIMER *
***********************
*
* Starts a keyboard interrupt.
*
KBIS    MVI   A,:FF     Init. 16.32 msec
        STA   TICCC     Load timer 4 (keyboard)
        RET 
*
********************
* LOAD SOUND TIMER *
********************
*
* Starts a sound interrupt.
*
SNDIS   MVI   A,:50     Init. 5.12 msec
        STA   TICCB     Load timer 3 (sound)
        RET 
*
***************************
* CLOCK INTERRUPT (RST 7) *
***************************
*
* Triggered every 20 msec by the TV page
* blanking signal.
* Decrements timer 01BE/BF until 0. Checks also
* the contents of cursor clock timer 01C0. It is
* decremented, if it becomes 0, the timer is re-
* loaded and the cursor is flashed.
*
* Exit: All registers preserved.
*
CLKINT  PUSH  PSW
        PUSH  B
        PUSH  D
        LDA   CHOPRT    Get current int. mask
        PUSH  PSW       and remember it
        MVI   A,:04
        STA   TICC8     Set stack interrupt only
        EI  
        LHLD  TIMER     Get timer contents
        MOV   A,H
        ORA   L
        JZ    CKI10     If timer = 0
        DCX   H         Else:
        SHLD  TIMER     decrement timer
CKI10   LXI   H,CURCLK  Addr. cursor clock
        DCR   M         Decr. clock
        JNZ   INTRM     Return if <>0
        MVI   M,:0F     Load 20 ms flash time
        RST   5         Flash cursor
        DATA  :12
*
* GENERAL INTERRUPT RETURN:
*
* Restores interrupt mask and all registers.
*
* Entry: Interrupt mask and all registers on stack.
*
INTRM   POP   PSW       Get old int. mask
        DI  
        STA   TICC8     Restore int. mask
        STA   CHOPRT    and remember it.
        EI  
        POP   D
        POP   B
        POP   PSW
        POP   H
        RET 
*
**************************
* ENABLE CLOCK INTERRUPT *
**************************
*
CLKEI   PUSH  B
        LXI   B,:FF80   Enable clock interrupt
        JMP   INTCH     Update int.mask
*
**************************
* STACK INTERRUPT (RST2) *
**************************
*
* This routine handles an interrupt caused by the
* stack overflow hardware logic.
*
SPINT   LXI   SP,STTOP  Reset stackpointer
        XRA   A
        STA   GOSPT     No running inputs
        STA   ERSFL     No encoding of stored line
        CALL  MPT30     Input from keyboard, reload
 REM timers sound/keyb
        EI  
        MVI   A,:16
        JMP   ERROR     Run error 'STACK OVERFLOW'
*
*
*
*
*  ===============
*** ERROR HANDLER ***
*  ===============
*
*
******************
* ERROR HANDLING *
******************
*
* Produces an error message and other information
* about errors.
*
* Entry: A:  Error message number.
*        BC: Latest position in EBUF or textbuffer.
* Exit:  If during input: Restart input statement.
*        If during encoding: Back to ELINA (C93C)
*                            for handling.
*        Else: Restart Basic (direct mode).
*
ERROR   MOV   B,A       Save error pointer
        XRA   A
        STA   OTSW      Set output screen/RS232
        CALL  SELB0     Select ROM bank 0
        DBL   :0000
        NOP 
        NOP 
        LDA   RUNF      Get RUNF
        ORA   A
        JNZ   ERRRU     If run-time error
        JMP   ERRCO     If compile-time error
*
* ENTRYPOINTS TO ERROR ROUTINES:
*
ERRSN   MVI   A,:17
        JMP   ERROR     Run 'SYNTAX ERROR'
*
ERROM   MVI   A,:13
        JMP   ERROR     Run 'OUT OF MEMORY'
*
ERRRA   MVI   A,:09
        JMP   ERROR     Run 'NUMBER OUT OF RANGE'
*
ERRTM   MVI   A,:14
        JMP   ERROR     Run 'TYPE MISMATCH'
*
ERROV   MVI   A,:03
        JMP   ERROR     Run 'OVERFLOW'
*
ERRD0   MVI   A,:06
        JMP   ERROR     Run 'DIVISION BY ZERO'
*
ERRBS   MVI   A,:05
        JMP   ERROR     Run 'SUBSCRIPT ERROR'
*
ERRNF   MVI   A,:00
        JMP   ERROR     Run 'NEXT WITHOUT FOR'
*
ERREL   MVI   A,:12
        JMP   ERROR     Run 'ERROR LINE RUN'
*
ERRLS   MVI   A,:08
        JMP   ERROR     Run 'STRING TOO LONG'
*
******************
* RUN-TIME ERROR *
******************
*
ERRRU   CALL  ERRMS     Print error message
        LDA   GOSPT     Get RDIPL
        ORA   A
        JNZ   ERT10     Jump if running inputs
        CALL  MSGIL     else: Print 'IN LINE <nr>'
        JMP   START     Re-enter BASIC
 REM
* If error in input:
 REM
ERT10   JMP   INPRS     (0) Back to restart input
*
***********************
* PRINT ERROR MESSAGE *
***********************
*
* Entry: B: Error message number.
* Exit:  BC preserved, AFDEHL corrupted.
*
ERRMS   CALL  COL0      Cursor to begin (next) line
        MOV   A,B       Get error number
        LXI   H,E?NF    Base of list error messages
        ADD   A         Multiply error nr *2
        MOV   E,A       and save offset
        MVI   D,:00
        DAD   D         Calculate table addr.
        MOV   E,M       ) Get addr of message
        INX   H         ) in DE
        MOV   D,M       )
        XCHG            and in HL
        CALL  PMSG      Print error message
        RET 
*
**********************
* COMPILE-TIME ERROR *
**********************
*
* Handles errors in direct mode. Only an error
* message is printed. Control is passed back to
* ELINA (C93C) except if it is an error during
* encoding of a stored line.
*
* Entry: A: Error number.
*        C: Points to last read character on
*           input line.
* Exit:  To Basic interpreter.
*
ERRCO   LDA   ERSFL     Get ERSFL
        CPI   :01       Error during encoding?
        JZ    ELARS     Then handle error
 REM
* Error in direct command:
 REM
        CALL  ERRMS     Print error message
        CALL  CRLF      Print car.ret
        JMP   ST20      Restart interpreter
*
 REM
********************************************
* PRINT LINE NUMBER IN WHICH ERROR OCCURED *
********************************************
*
* Prints car.ret if current line is a direct command
* Else, prints 'IN LINE <linenr>' and car.ret.
*
* Entry: None.
* Exit:  ABCDEHL preserved. F corrupted.
*        Z=1: direct command.
*
MSGIL   PUSH  H
        PUSH  PSW
        LHLD  CURRNT    Get start current line
        MOV   A,H
        ORA   L         Check if 0
        PUSH  PSW
        JZ    MIL10     If direct, print car.ret
        CALL  PMSGR     Else, print 'IN LINE'
        DBL   MSG02
        MOV   A,M       ) Get line nr in HL
        INX   H         )
        MOV   L,M       )
        MOV   H,A       )
        CALL  SLN10     (0) Print line number
MIL10   CALL  CRLF      Print car.ret
        POP   PSW
        POP   H
        MOV   A,H
        POP   H
        RET 
*
************************************
* ERROR MESSAGES INDIRECTION TABLE *
************************************
*
* The address points to the location where the
* string with the error messages can be found.
* Between brackets the error number.
*
* Run-time errors:
*ERITB
E?NF    DBL   ERMNF     (00) NEXT WITHOUT FOR
E?RG    DBL   ERMRG     (01) RETURN WITHOUT GOSUB
E?OD    DBL   ERMOD     (02) OUT OF DATA
E?OV    DBL   ERMOV     (03) OVERFLOW
E?US    DBL   ERMUS     (04) UNDEFINED LINE NUMBER
E?BS    DBL   ERMBS     (05) SUBSCRIPT ERROR
E?D0    DBL   ERMD0     (06) DIVISION BY ZERO
E?OS    DBL   ERMOS     (07) OUT OF STRING SPACE
E?LS    DBL   ERMLS     (08) STRING TOO LONG
E?RA    DBL   ERMNA     (09) NUMBER OUT OF RANGE
E?IN    DBL   ERMIN     (0A) INVALID NUMBER
E?LO0   DBL   ERML0     (0B) LOADING ERROR 0
E?LO1   DBL   ERML1     (0C) LOADING ERROR 1
E?LO2   DBL   ERML2     (0D) LOADING ERROR 2
E?LO3   DBL   ERML3     (0E) LOADING ERROR 3
E?UA    DBL   ERMUA     (0F) UNDEFINED ARRAY
E?NC    DBL   ERMNC     (10) COLOUR NOT AVAILABLE
E?OF    DBL   ERMOF     (11) OFF SCREEN
E?EL    DBL   ERMEL     (12) ERROR LINE RUN
 REM
* Compile/Run-time errors:
 REM
E?OM    DBL   ERMOM     (13) OUT OF MEMORY
E?TM    DBL   ERMTM     (14) TYPE MISMATCH
E?LN    DBL   ERMLN     (15) LINE NUMBER OUT OF
 REM .    RANGE
E?SO    DBL   ERMSO     (16) STACK OVERFLOW
 REM
* Compile-time errors:
 REM
E?SN    DBL   ERMSN     (17) SYNTAX ERROR
E?ID    DBL   ERMID     (18) COMMAND INVALID
E?CN    DBL   ERMCN     (19) CAN'T CONT
E?TC    DBL   ERMTC     (1A) LINE TOO COMPLEX
E?ST    DBL   ERMOM     (1B) OUT OF MEMORY
*
*******************************
* WAIT; part of RTALK (0EC6D) *
*******************************
*
* Entry: HL: Wait time.
*
 REM
* If wait:
* As old routine, but HL replaced by DE.
 REM
RTK20   DCX   D         ]
        MOV   A,D       ]
        ORA   E         ]
        JNZ   RTK20     ] Wait for DE=0
        RET             ]
        DATA  :FF       ]
*
*
*
        UNL
TICC8   EQU   :FFF8
CHOPRT  EQU   :005F
CURCLK  EQU   :01C0
KBXCT   EQU   :01C1
I7USA   EQU   :0070
KBINT   EQU   :D578
I6USA   EQU   :006E
SCRST   EQU   :C6FD
I5USA   EQU   :006C
MARST   EQU   :C6C0
I4USA   EQU   :006A
SNTMP   EQU   :D755
I3USA   EQU   :0068
I2USA   EQU   :0066
UTRST   EQU   :C70E
I1USA   EQU   :0064
MOVE    EQU   :DE4F
TICCC   EQU   :FFFC
TICCB   EQU   :FFFB
TIMER   EQU   :01BE
STTOP   EQU   :F900
GOSPT   EQU   :0117
ERSFL   EQU   :0122
MPT30   EQU   :D743
OTSW    EQU   :0131
SELB0   EQU   :CEE4
RUNF    EQU   :0118
START   EQU   :C814
INPRS   EQU   :E350
COL0    EQU   :DD55
PMSG    EQU   :DAD4
ELARS   EQU   :C957
CRLF    EQU   :DD5E
ST20    EQU   :C823
CURRNT  EQU   :0100
PMSGR   EQU   :DAFF
MSG02   EQU   :DB7F
SLN10   EQU   :EFB4
ERMNF   EQU   :DC1C
ERMRG   EQU   :DC2C
ERMOD   EQU   :DC33
ERMOV   EQU   :DC3E
ERMUS   EQU   :DC50
ERMBS   EQU   :DC5C
ERMD0   EQU   :DC68
ERMOS   EQU   :DC95
ERMLS   EQU   :DC9D
ERMNA   EQU   :DCD8
ERMIN   EQU   :DCB2
ERML0   EQU   :DCFA
ERML1   EQU   :DCFE
ERML2   EQU   :DD02
ERML3   EQU   :DD06
ERMUA   EQU   :DCF1
ERMNC   EQU   :DCC2
ERMOF   EQU   :DCB7
ERMEL   EQU   :DD12
ERMOM   EQU   :DC47
ERMTM   EQU   :DC8A
ERMLN   EQU   :DCD6
ERMSO   EQU   :DC38
ERMSN   EQU   :DC23
ERMID   EQU   :DC79
ERMCN   EQU   :DCA9
ERMTC   EQU   :DCE3
        LST
        END 
